package com.teach.controller;

import com.teach.entity.ResponseEntity;
import com.teach.entity.TbUser;
import com.teach.service.TbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Api(tags = "用户的controller")
public class UserController {
    @Resource
    private TbUserService tbUserService;
    /**
     * 获取token
     * @param request
     * @return
     */
    public String getToken(HttpServletRequest request){
        String token = request.getHeader("token");
        return token;
    }

    /**
     * 用户名密码登录
     * @return
     */
    @ApiOperation("登录的方法")
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody TbUser tbUser){
        return tbUserService.login(tbUser);
    }

    /**
     * 用户列表
     * @return
     */
    @ApiOperation("用户列表")
    @PostMapping("/userList")
    public ResponseEntity userList(@RequestBody TbUser tbUser){
        return tbUserService.userList(tbUser);
    }

    /**
     * 添加用户
     * @param tbUser
     * @return
     */
    @ApiOperation("用户添加")
    @PostMapping("/userSave")
    public ResponseEntity userSave(@RequestBody TbUser tbUser,HttpServletRequest request){
        String token = this.getToken(request);
        return  tbUserService.userSave(tbUser,token);
    }

    /**
     * 修改用户的密码
     * @param tbUser
     * @return
     */
    @ApiOperation("更新密码")
    @PostMapping("/updatePwd")
    public ResponseEntity updatePwd(@RequestBody TbUser tbUser, HttpServletRequest request){
        String requestURI = request.getRequestURI();
        //获取token
        String token = request.getHeader("token");
        return tbUserService.updatePwd(tbUser,token);
    }

    /**
     * 用户删除
     * @param
     * @return
     */
    @ApiOperation("删除用户")
   @PostMapping("/userDel")
    public ResponseEntity userDel(@RequestBody TbUser tbUser,HttpServletRequest request){
       String token = this.getToken(request);
       return tbUserService.userDel(tbUser,token);
   }
}
